//
//  ViewController.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/3.
//  Copyright © 2017年 梅津山. All rights reserved.
//RCOEvNGnfIv6C0DBhzqqq7TM
//MEy008sXicRrl8vIIc0FEl8o

#import "ViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <LeanCloudSocial/AVOSCloudSNS.h>
#import "LYRegisterViewController.h"
#import "NowController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


}



//微博登录
- (IBAction)loginbtn:(id)sender {
   [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
       if (object) {
         //  NSLog(@"%@",object);
           
          
           [AVUser logInWithUsernameInBackground:object[@"username"]  password:@"123" block:^(AVUser *user, NSError *error) {
               if (user != nil) {
                   NowController *now = [NowController sharedNow];
                   [self presentViewController:now  animated:YES completion:nil];
                   
               } else {
                   AVUser *user = [AVUser user];// 新建 AVUser 对象实例
                   user.username = object[@"username"];// 设置用户名
                   user.password = @"123";// 设置密码
                   [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                       if (succeeded) {
                           // 注册成功
                           [AVUser logInWithUsernameInBackground:user.username password:@"123" block:^(AVUser *user, NSError *error) {
                               if (user != nil) {
                                   NowController *now = [NowController sharedNow];
                                   [self presentViewController:now  animated:YES completion:nil];
                                   // LeanCloud - 获取当前用户
                                   AVUser *currentUser = [AVUser currentUser];
//                                   [currentUser setObject:object[@"username"] forKey:@"name"];
                                   [currentUser setObject:object[@"avatar"] forKey:@"image"];
                                   NSLog(@"%@",object[@"avatar"]);
                                   [currentUser saveInBackground];
                                   // owner 字段为 Pointer 类型，指向 _User 表
                                   //[todoFolder setObject:currentUser forKey:@"owner"];
                                   //[todoFolder setObject:object[@"username"]  forKey:@"name"];
                                   
//                                   NSData *data = UIImagePNGRepresentation(object[@"avatar"]);
//                                 //  AVFile *file = [AVFile fileWithData:data];
//                                   NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
//                                   [todoFolder setObject:object[@"avatar"] forKey:@"imageT"];
//                                 
//                                   [todoFolder saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
//                                       NSLog(@"%@",error);
//                                   }];// 保存到云端
                               } else {
                                   
                               }
                           }];
                       } else {
                           // 失败的原因可能有多种，常见的是用户名已经存在。
                       }
                   }];
                   
               }
           }];
       }
       
   } toPlatform:(AVOSCloudSNSSinaWeibo)];

    
}
//手机号一键登录
- (IBAction)registerBtn:(UIButton *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController *lyVc =[sb instantiateViewControllerWithIdentifier:@"register"];
    //    LYViewController *ly = [[LYViewController alloc]init];
    [self presentViewController:lyVc  animated:YES completion:nil];
    
    
}
- (IBAction)QQBtn:(UIButton *)sender {
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        NSLog(@"%@",object);
        if (object) {
            NowController *now = [NowController sharedNow];
            [self presentViewController:now animated:YES completion:nil];;            
        }
    } toPlatform:(AVOSCloudSNSQQ)];
    
    
}

@end
